class_name StateMachine extends Node


@export var starting_state: String
var states: Dictionary
var current_state: State

func _ready() -> void:
	for child: Node in get_children():
		var state: State = child as State
		if not state:
			continue
		states[state.name] = state
		state._state_machine = self
	if not starting_state.is_empty():
		pass
		

func transition(state_name: String) -> void:
	if state_name.is_empty() or states.is_empty() or not states.has(state_name):
		return
	if current_state:
		current_state.exit()
	current_state = states[state_name]
	current_state.enter()
	
	
func _physics_process(delta: float) -> void:
	if not current_state:
		return
	current_state.process(delta)


func _input(event: InputEvent) -> void:
	if not current_state:
		return
	current_state.input(event)
